summaryrefslogtreecommitdiff
path: root/frontend/app/download/[...path]/route.ts
blob: 966a89e10765392e8d41b5471435cc86bc0f8656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { NextRequest, NextResponse } from 'next/server'
import { Drive_stat } from '@/lib/drive_server'

// GET /download/[...path] - Download file by path (redirects to blob endpoint)
export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ path: string[] }> }
) {
  try {
    const { path } = await params
    
    // Reconstruct the full path
    const fullPath = '/' + path.join('/')
    
    // Get filename from path for the download
    const filename = path[path.length - 1] || 'download'
    
    // Get blob ID using Drive_stat
    const blobId = await Drive_stat(fullPath)
    
    // Redirect to blob endpoint with filename - preserve original host
    // Use X-Forwarded-Host or Host header to get the correct host
    const forwardedHost = request.headers.get('x-forwarded-host')
    const host = forwardedHost || request.headers.get('host') || new URL(request.url).host
    const protocol = request.headers.get('x-forwarded-proto') || (new URL(request.url).protocol.replace(':', ''))
    const redirectUrl = `${protocol}://${host}/blob/${blobId}?filename=${encodeURIComponent(filename)}`
    
    return NextResponse.redirect(redirectUrl)
    
  } catch (error) {
    console.error('Download error:', error)
    return new NextResponse(
      error instanceof Error ? error.message : 'File not found',
      { status: 404 }
    )
  }
}